package com.cheer.ba.view.fragment;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.model.MineFragItemModel;
import com.cheer.ba.utils.ImageUtils;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.base.BaseCommFrg;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.MyListView;
import com.zbar.lib.activity.CaptureActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 车友圈
 */
public class FocusFrg extends BaseCommFrg implements AdapterView.OnItemClickListener {

    @BindView(R.id.id_focus_listv)
    MyListView mFocusListv;

    private List<MineFragItemModel> mItems = new ArrayList<MineFragItemModel>();

    CommonAdapter<MineFragItemModel> mFocusAdapter;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.frg_focus, null, false));
    }

    @Override
    public void initView() {
        MineFragItemModel item = new MineFragItemModel(getString(R.string.car_circle), R.drawable.ic_car_circle, "", "", "", false, true, true);
        MineFragItemModel item1 = new MineFragItemModel(getString(R.string.point_store), R.drawable.ic_point_store, "", "", "", false, false, true);
        MineFragItemModel item2 = new MineFragItemModel(getString(R.string.scan_again), R.drawable.ic_scan, "", "", "", false, true, true);
        MineFragItemModel item3 = new MineFragItemModel(getString(R.string.ranking_list), R.drawable.ic_ranking_list, "", "", "", false, false, true);
        MineFragItemModel item4 = new MineFragItemModel(getString(R.string.near_person), R.drawable.ic_car_circle, "", "", "", false, true, true);
        mItems.add(item);
        mItems.add(item1);
        mItems.add(item2);
        mItems.add(item3);
        mItems.add(item4);
    }

    @Override
    public void initData() {
        mFocusAdapter = new CommonAdapter<MineFragItemModel>(frg, mItems, R.layout.adp_mine_frg) {
            @Override
            public void convert(ViewHolder helper, MineFragItemModel item) {
                if (item.getDrawable() != 0) {
                    helper.getViewById(R.id.id_icon_img).setVisibility(View.VISIBLE);
                    helper.setImageResource(R.id.id_icon_img, item.getDrawable());
                } else {
                    helper.getViewById(R.id.id_icon_img).setVisibility(View.GONE);
                }
                helper.setText(R.id.id_name_txt, item.getName());
                //描述
                TextView mTxtDesc = (TextView) helper.getViewById(R.id.id_right_des_txt);
                //头像
                ImageView mHeadImg = (ImageView) helper.getViewById(R.id.id_person_head_img);
                //通知数量
                TextView mTxtNum = (TextView) helper.getViewById(R.id.id_contact_num_update);
                if (!StringUtils.isEmpty(item.getmDesc())) {
                    mTxtDesc.setVisibility(View.VISIBLE);
                    mTxtDesc.setText(item.getmDesc());
                } else {
                    mTxtDesc.setVisibility(View.GONE);
                }
                if (!StringUtils.isEmpty(item.getmHead())) {
                    mHeadImg.setVisibility(View.VISIBLE);
                    ImageUtils.showRoundImg(frg, Session.getUserPicUrl(), R.drawable.ic_user_head, R.drawable.ic_user_head, mHeadImg);
                } else {
                    mHeadImg.setVisibility(View.GONE);
                }
                if (!StringUtils.isEmpty(item.getmNum())) {
                    mTxtNum.setVisibility(View.VISIBLE);
                    mTxtNum.setText(item.getmNum());
                } else {
                    mTxtNum.setVisibility(View.GONE);
                }
                if (item.isArrow()) {
                    helper.getViewById(R.id.id_right).setVisibility(View.VISIBLE);
                } else {
                    helper.getViewById(R.id.id_right).setVisibility(View.GONE);
                }
                if (item.isDivider()) {
                    helper.getViewById(R.id.id_line_v).setVisibility(View.VISIBLE);
                    helper.getViewById(R.id.id_divider_v).setVisibility(View.GONE);
                } else {
                    helper.getViewById(R.id.id_line_v).setVisibility(View.GONE);
                    helper.getViewById(R.id.id_divider_v).setVisibility(View.VISIBLE);
                }
            }
        };
        mFocusListv.setAdapter(mFocusAdapter);
        mFocusListv.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            //车友圈
            case 0:

                break;
            //积分商城
            case 1:

                break;
            //扫一扫
            case 2:
                if (isLogin(true)) {
                    checkPermissCamer();
                }
                break;
            //排行榜
            case 3:

                break;
            //附近
            case 4:

                break;
        }
    }

    /**
     * 检测用户是否开启权限
     */
    private void checkPermissCamer() {
        String[] perms = {Manifest.permission.CAMERA};
        if (!mayRequestPermission(perms)) {
            return;
        }
        toScan();
    }

    /**
     * 进入扫描二维码界面
     */
    private void toScan() {
        Intent scan = new Intent(frg, CaptureActivity.class);
        startActivityForResult(scan, IntentCode.Scan.SCAN_REQUEST_CODE);
    }

    @Override
    public void requestPermissionResult(boolean allowPermission) {
        if (allowPermission) {
            toScan();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == IntentCode.Scan.SCAN_REQUEST_CODE && resultCode == frg.RESULT_OK) {
            if (null == data) {
                return;
            }
            String scanResult = data.getExtras().getString("result");
            ToastUtil.showToast(scanResult + "1111111111");
            try {
                JSONObject object = new JSONObject(scanResult);
                Bundle bundle = new Bundle();
                if (object.has("uid")) {
                    bundle.putString("custId", object.getString("uid"));
                    ToastUtil.showToast("好友");
                } else {
                    ToastUtil.showToast(scanResult);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
